package com.kuzlak.cms.module.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.bean.Result;
import com.kuzlak.cms.config.ConstantHeader;
import com.kuzlak.cms.constant.ResultConstant;
import com.kuzlak.cms.module.sys.bean.SettingsVO;
import com.kuzlak.cms.module.sys.entity.SysSettings;
import com.kuzlak.cms.module.sys.service.SysSettingsService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */
@Tag(name = "参数管理")
@RestController
@RequestMapping("/api/setting")
public class SettingsController {
    @Autowired
    private SysSettingsService service;


    @Operation(summary= "参数列表")
    @PostMapping("list")
    public Result list(
            @RequestParam(name = "page", defaultValue = "0") int page,
            @RequestParam(name = "pagesize", defaultValue = "10") int pagesize,
            @Parameter(description= "名称") @RequestParam(name = "name", defaultValue = "") String name,
            @Parameter(description= "状态") @RequestParam(name = "status", defaultValue = "0") int status,
            @Parameter(description= "参数类型") @RequestParam(name = "isKey", defaultValue = "") String isKey

    ) {
        Result gridResult = new Result();
        MyPage page1 = service.findByPage(pagesize, page, name, status, isKey);


        gridResult.setData(page1);
        gridResult.setCode(ResultConstant.SUCSESS);

        return gridResult;
    }

    @Operation(summary= "获取参数值")
    @PostMapping("info")
    public Result info() {
        Map<String, Object> list = service.findByKeys();
        ConstantHeader.setSetting(list);
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }

    @Operation(summary= "添加或编辑")
    @PostMapping("edit")
    public Result edit(@RequestBody  @Valid SettingsVO dataVo
    ) {
        SysSettings data = BeanUtil.copyProperties(dataVo, SysSettings.class);
        Long id = data.getId();
        if (id == null) {
            data.setId(0L);
        }
        try {


            boolean flag = false;
            if (id > 0) {
                data.setId(id);
                flag = service.updateById(data);
            } else {
                flag = service.save(data);
            }

            return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);

        }catch (Exception e){
            return Result.failure(ResultConstant.FAIL);
        }
    }

    @Operation(summary= "状态修改")
    @PostMapping("status")
    public Result status(
            @RequestParam(name = "status", defaultValue = "0") int status,
            @RequestParam(name = "id", defaultValue = "0") long id
    ) {
        Result result = new Result();
        if (id > 0) {

            LambdaUpdateWrapper<SysSettings> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(SysSettings::getStatus, status);
            updateWrapper.eq(SysSettings::getId, id);
            boolean update = service.update(updateWrapper);
            if (update) {

                result.setCode(ResultConstant.SUCSESS);
            } else {
                result.setCode(ResultConstant.FAIL);
            }
        } else {
            result.setCode(ResultConstant.EMPTIY);
        }

        return result;
    }

    @Operation(summary= "删除")
    @PostMapping("delete")
    public Result status(
            @RequestParam(name = "id", defaultValue = "0") long id
    ) {
        Result result = new Result();
        if (id > 0) {
            boolean update = service.deleteById(id);
            if (update) {

                result.setCode(ResultConstant.SUCSESS);
            } else {
                result.setCode(ResultConstant.FAIL);
            }
        } else {
            result.setCode(ResultConstant.EMPTIY);
        }

        return result;
    }
}

